/**
  ******************************************************************************
  * File Name          : prodrttsk.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include "prodrt.h"
#include "gv.h"
#include "modbus.h"
#include <string.h>

TaskHandle_t xProductionRT_Thd = NULL;
StaticTask_t xProductionRT_Tcb;
StackType_t  xProductionRT_Stk[PRODUCTIONRT_STACK_SIZE];

Ethernet_SendQueStru prodrt_txbuf = {
  .sn = ETH_SOCK_NUM_PRODRT,
  .type = 1,
  .dest_ip = {192,168,6,232},
  .dest_port = 8001,
  .len = 23,
  .buf = "make test socket in 1\r\n",
};

static Ethernet_FeedbackDataStru prodrt_rxbuf;

static volatile uint8_t tcp_connected = 0;

static void tcp_client_connection_keeps(void)
{
  uint8_t sr;
  static TickType_t xLastWakeTime = 0;

  if(((xTaskGetTickCount() - xLastWakeTime) >= 1000) || (tcp_connected == 0))
  {
    xLastWakeTime = xTaskGetTickCount();

    sr = ethif_get_socket_sr(ETH_SOCK_NUM_PRODRT);
    switch(sr)
    {
      case SOCK_UDP:
      case SOCK_IPRAW:
      case SOCK_MACRAW:
      case SOCK_CLOSE_WAIT:
        tcp_connected = 0;
        ethif_close(ETH_SOCK_NUM_PRODRT); 

      case SOCK_CLOSED:
        tcp_connected = 0;
        ethif_socket(ETH_SOCK_NUM_PRODRT, Sn_MR_TCP, ETH_SOCK_LCPORT_PRODRT, 0);

      case SOCK_INIT:
        tcp_connected = 0;
        ethif_connect(ETH_SOCK_NUM_PRODRT, gv.hr.meas_pa.eth_prodrt_ip, gv.hr.meas_pa.eth_prodrt_port);
        break;

      case SOCK_SYNSENT:
        tcp_connected = 0;
        break;

      case SOCK_ESTABLISHED:
        tcp_connected = 1;
        break;

      default:
        break;
    }
  }
}

static void feedback_process(TickType_t blocktime)
{
  Ethernet_FeedbackCtrlStru ctrl;
  QueueSetMemberHandle_t activated_qhd;

  activated_qhd = xQueueSelectFromSet(prodrt_ethfb_qshd, blocktime);
  if(activated_qhd == prodrt_ethfb_qhd[CQUE])         /* 中断消息 */
  {
    if(xQueueReceive(activated_qhd, &ctrl, 0) !=NULL)
    {
      switch(ctrl.type)
      {
        case ETH_FB_CONNECTED:
          tcp_connected = 1;
          break;

        case ETH_FB_DISCONNECTED:
          tcp_connected = 0;
          break;

        case ETH_FB_TIMEOUT:
          tcp_connected = 0;
          break;

        default:
          break;
      }
    }
  }
  else if(activated_qhd == prodrt_ethfb_qhd[DQUE])    /* 数据消息接收 */
  {
    if(xQueueReceive(activated_qhd, &prodrt_rxbuf, 0) !=NULL)
    {
//      prodrt_txbuf.len = 0;
//      if(modbus_tcp_process(prodrt_rxbuf.buf, prodrt_rxbuf.len, prodrt_txbuf.buf, &prodrt_txbuf.len) != 0)
//      {
//        if(prodrt_txbuf.len != 0){
//          ethif_tosend(&prodrt_txbuf);
//        }
//      }
    }
  }
}

void vProductionRT_Task(void * pvParameters)
{
  uint8_t last_second = 0;
  
//  modbus_init();
  
  while(1)
  {
    tcp_client_connection_keeps();

    feedback_process(500);
    
    if(((sys_datatime.second%5) == 0) && (last_second != sys_datatime.second))
    {
      last_second = sys_datatime.second;
      prodrt_data_upload(&prodrt_txbuf);
    }
  }
}
